﻿using Enyim.Caching;
using Ayatta.Contract;
using Web.My.Models;
using System.Web.Mvc;
using Web.Base.Security;
using Ayatta.Contract.Domain;
using Ayatta.Contract.Component;

namespace Web.My.Controllers
{
    public class InfoController : MyBaseController
    {
        public InfoController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index()
        {
            var model = new InfoControllerModel.Index();

            var identity = User.Identity.Identity();
            var user = PassportService.GetUser(identity.Id, true);
            model.User = user;

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(string nickname, UserProfile profile)
        {
            var result = new SkResult<string>();
            var identity = User.Identity.Identity();

            var old = PassportService.GetUserProfile(identity.Id);
            var authenticated = old.Authenticated;
            if (TryUpdateModel(old))
            {
                old.Authenticated = authenticated;
                PassportService.UpdateUserNickName(identity.Id, nickname);
                var status = PassportService.UpdateUserProfile(old);
                result.Status = status;
                if (!status)
                {
                    result.Message = "更新失败，请重试！";
                }
            }
            return Json(result);
        }

    }
}
